<template>
	<view class="container">
		<view class="order-address">
			<view class="info-top f-a-baseline">
				<view class="name">{{info.name}}</view>
				<view class="phone">{{info.user_phone}}</view>
			</view>
			<view class="info-bottom f-a-start">
				<image src="/static/order/icon-address.png" class="icon-address"></image>
				<view class="detail">{{info.user_address}}</view>
			</view>
		</view>
		<image src="/static/order/envelope-line.png" class="envelope-line"></image>

		<view class="order-product">
			<view class="">
				<view class="comp-product" v-for="(goods,i) in info.goods" :key="goods.goods_id">
					<orderProduct :goods="goods" statusColor="#FFC046"></orderProduct>
					<view class="item-btns f-j-end">
						<template v-if="goods.status==1"><!-- 待发货 -->
							<button class="btn f-center" @click="callPhone">联系客服</button>
						</template>

						<template v-if="goods.status==2"><!-- 待收货 -->
							<button class="btn f-center" @click="callPhone">联系客服</button>
							<button class="btn f-center" @click="btnOperation(goods.ordergoods_id,'showReceipt',info)">确认收货</button>
						</template>

						<template v-if="goods.status==3"><!-- 已完成 -->
							<button class="btn f-center" @click="callPhone">联系客服</button>
						</template>

						<template v-if="goods.status==1||goods.status==2||goods.status==3">
							<button class="btn f-center" @click="btnOperation(goods.ordergoods_id,'showRefund')">立即退款</button>
						</template>

						<template v-if="goods.status==-2"><!-- 退款中 -->
							<button class="btn f-center" @click="cancelRefundOrder(goods.ordergoods_id)">取消退款</button>
						</template>
					</view>
				</view>
			</view>

			<view class="order-other">
				<view class="rows f-c-between">
					<view class="label">优惠券</view>
					<view class="value">暂无可用</view>
				</view>
				<view class="rows f-c-between">
					<view class="label">运费</view>
					<view class="value">0元</view>
				</view>
			</view>
			<view class="order-price f-c-between">
				<view class="label">订单总价</view>
				<view class="value">¥{{info.total_price}}</view>
			</view>
		</view>

		<view class="order-info">
			<view class="title">订单信息</view>
			<view class="content">
				<view class="rows f-a-center">
					<view class="label">订单编号：</view>{{info.order_no}}
				</view>
				<view class="rows f-a-center">
					<view class="label">支付类型：</view>微信支付
				</view>
				<view class="rows f-a-center">
					<view class="label">创建时间：</view>{{info.createtime_text}}
				</view>
				<view v-if="info.pay_time_text" class="rows f-a-center">
					<view class="label">付款时间：</view>{{info.pay_time_text}}
				</view>
			</view>
		</view>

		<view v-if="info.status==0" class="footer-fixed f-j-end">
			<button class="btn link" @click="showCancel=true">取消订单</button>
			<button class="btn" @click="goPay">去付款</button>
		</view>

		<myModal :show="showCancel" title="提示" content="确定要取消该订单吗？" @cancel="showCancel=false" @confirm="confirmCancel">
		</myModal>
		<myModal :show="showReceipt" title="提示" content="确认收到货了吗？" @cancel="showReceipt=false" @confirm="confirmReceipt">
		</myModal>
		<myModal :show="showRefund" title="提示" content="确定要申请退款吗？" @cancel="showRefund=false" @confirm="confirmRefund">
		</myModal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				order_id: '',
				info: {},
				tempGoodsId: null,
				showCancel: false,
				showReceipt: false,
				showRefund: false,
				tradeObj:{},
			}
		},
		onLoad({
			order_no
		}) {
			this.order_no = order_no
			this.getOrderDetail()
		},
		methods: {
			//获取订单详情
			getOrderDetail() {
				this.$api.get_order_detail({
					order_no: this.order_no
				}).then(res => {
					this.info = res
				})
			},

			//取消订单
			confirmCancel() {
				this.$api.cancel_order({
					order_id: this.info.id
				}).then(res => {
					this.$showToast('已取消')
					this.showCancel = false
					this.getOrderDetail()
				})
			},

			//去支付
			goPay() {
				this.$u.debounce(() => {
					this.$api.pay_order({
						order_id: this.info.id,
						// #ifdef MP-WEIXIN
						client_type: 'Wechat'
						// #endif
						// #ifdef APP
						client_type: ' APP'
						// #endif
					}).then(data => {
						this.$util.payUtil(data).then(res => {
							uni.navigateTo({
								url: '/pages/cart/payResult'
							})
						})
					})
				}, 300)
			},

			//操作按钮
			btnOperation(ordergoods_id, type, info) {
				this.tempGoodsId = ordergoods_id
				this[type] = true
				if(info){
					this.tradeObj = info
				}
			},

			//确认收货
			confirmReceipt() {
				let _this = this
				// #ifdef MP-WEIXIN
				//拉起确认收货组件
				if (wx.openBusinessView) {
				  wx.openBusinessView({
				    businessType: 'weappOrderConfirm',
				    extraData: {
				      merchant_id: '1717292533',
				      merchant_trade_no: _this.tradeObj.order_no,
				    },
				    success() {
						console.log('chengg',_this.tradeObj.order_no)
				      _this.$api.get_receipt_order({
				      	ordergoods_id: _this.tempGoodsId
				      }).then(res => {
				      	_this.showReceipt = false
				      	_this.getOrderDetail()
				      })
				    },
				    fail(err) {
						console.log('shibai',err)
				      //dosomething
				    },
				    complete() {
				      //dosomething
				    }
				  });
				} else {
				  //引导用户升级微信版本
				  _this.$showToast('请先升级微信版本')
				}
				// #endif
				
				// #ifndef MP-WEIXIN
				this.$api.get_receipt_order({
					ordergoods_id: this.tempGoodsId
				}).then(res => {
					this.showReceipt = false
					this.getOrderDetail()
				})
				// #endif
			},

			//申请退款
			confirmRefund() {
				this.$api.get_refund_order({
					ordergoods_id: this.tempGoodsId
				}).then(res => {
					this.$showToast('申请成功，待商家处理')
					this.showRefund = false
					this.getOrderDetail()
				})
			},

			//取消退款
			cancelRefundOrder(ordergoods_id) {
				this.$api.cancel_refund_order({
					ordergoods_id
				}).then(res => {
					this.$showToast('退款已取消')
					this.getOrderDetail()
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #F4F7F7;
	}
</style>
<style lang="scss" scoped>
	.container {
		padding-bottom: 200rpx;
	}

	.order-address {
		background: #FFFFFF;
		padding: 28rpx 106rpx 24rpx 44rpx;

		.info-top {
			padding-left: 4rpx;

			.name {
				font-weight: 500;
				// font-size: 32rpx;
				font-size: calc(32rpx * 1.2);
				color: #333333;
			}

			.phone {
				font-weight: 400;
				// font-size: 24rpx;
				font-size: calc(24rpx * 1.4);
				color: #676767;
				margin-left: 14rpx;
			}
		}

		.info-bottom {
			margin-top: 10rpx;

			.icon-address {
				min-width: 34rpx;
				max-width: 34rpx;
				height: 34rpx;
				margin-right: 8rpx;
				padding-top: 4rpx;
			}

			.detail {
				font-weight: 400;
				// font-size: 24rpx;
				font-size: calc(24rpx * 1.4);
				color: #333333;
				// line-height: 32rpx;
				line-height: calc(32rpx * 1.4);
			}
		}
	}

	.envelope-line {
		width: 100%;
		height: 4rpx;
		display: block;
	}

	.order-product {
		background: #FFFFFF;
		box-shadow: 0px 0px 18rpx 0px rgba(0, 0, 0, 0.11);
		padding: 18rpx 46rpx 0;
		margin-top: 18rpx;

		.comp-product {
			padding: 24rpx 4rpx;
			border-bottom: 1px solid #dddcdc;

			&:last-child {
				border-bottom: 0;
			}

			.item-btns {
				margin-top: 12rpx;

				.btn {
					// width: 152rpx;
					// height: 54rpx;
					border-radius: 28rpx;
					border: 1px solid #FFC046;
					font-weight: 500;
					// font-size: 24rpx;
					font-size: calc(24rpx * 1.4);
					line-height: calc(26rpx * 1.4);
					color: #FFC046;
					background-color: #FFFFFF;
					padding: 8rpx 26rpx;
					margin-left: 20rpx;
				}

				.fill {
					background-color: #FFC046;
					color: #FFFFFF;
				}
			}
		}
	}

	.order-other {
		padding-bottom: 10rpx;
		border-bottom: 1px solid #dddcdc;

		.rows {
			padding: 8rpx 0;
			font-weight: 400;
			// font-size: 22rpx;
			font-size: calc(22rpx * 1.4);
			color: #A1A1A1;

			.value {
				color: #676767;
			}
		}
	}

	.order-price {
		padding: 18rpx 0 24rpx;

		.label {
			font-weight: 400;
			// font-size: 24rpx;
			font-size: calc(24rpx * 1.4);
			color: #333333;
		}

		.value {
			font-weight: 500;
			font-size: 40rpx;
			color: #FF6413;
		}
	}

	.order-info {
		background: #FFFFFF;
		box-shadow: 0px 0px 18rpx 0px rgba(0, 0, 0, 0.11);
		padding: 40rpx 44rpx 0;
		margin-top: 24rpx;

		.title {
			font-weight: 500;
			// font-size: 28rpx;
			font-size: calc(28rpx * 1.4);
			color: #333333;
		}

		.content {
			padding: 20rpx 0 24rpx;

			.rows {
				margin-bottom: 16rpx;
				font-weight: 400;
				// font-size: 22rpx;
				font-size: calc(22rpx * 1.4);
				color: #A1A1A1;

				.label {
					width: 160rpx;
				}
			}
		}
	}

	.footer-fixed {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 24rpx;
		background-color: #FFFFFF;

		.btn {
			width: 188rpx;
			height: 62rpx;
			background: #FFC046;
			border-radius: 35rpx;
			font-weight: 500;
			font-size: 24rpx;
			font-size: calc(24rpx * 1.2);
			color: #FFFFFF;
			margin-left: 20rpx;
			border: 1px solid #FFC046;

			&.link {
				background: #FFFFFF;
				color: #FFC046;
			}
		}
	}
</style>